using Microsoft.Xna.Framework;
using Terraria;
using Terraria.Audio;
using Terraria.ID;
using Terraria.ModLoader;

namespace HardcoreGame.Content.Projectiles.Ammo
{
    public class ChloroSlingBullet : ModProjectile
    {
        public override void SetDefaults()
        {
            Projectile.friendly = true;
            Projectile.hostile = false;
            Projectile.DamageType = DamageClass.Ranged;
            Projectile.penetrate = 1;
            Projectile.width = 14;
            Projectile.height = 14;
            Projectile.extraUpdates = 1;
            Projectile.ignoreWater = true;
        }

        public override void AI()
        {
            Projectile.rotation += Projectile.velocity.X * 0.1f;

            if (Projectile.velocity.X > 18f)
            {
                Projectile.velocity.X = 18f;
            }
            if (Projectile.velocity.X < -18f)
            {
                Projectile.velocity.X = -18f;
            }

            if (Projectile.velocity.Y > 18f)
            {
                Projectile.velocity.Y = 18f;
            }
            if (Projectile.velocity.Y < -18f)
            {
                Projectile.velocity.Y = -18f;
            }

            int num74 = Dust.NewDust(new Vector2(Projectile.position.X, Projectile.position.Y), Projectile.width, Projectile.height, 40, 0f, 0f, 0, default, 1f);
            Main.dust[num74].noGravity = true;
            Main.dust[num74].scale = 1.3f;
            Dust dust3 = Main.dust[num74];
            dust3.velocity *= 0.5f;
        }

        public override void OnKill(int timeLeft)
        {
            Player player = Main.player[Projectile.owner];
            Projectile.width = 1;
            Projectile.height = 1;
            Projectile.position.X = Projectile.position.X - Projectile.width / 2;
            Projectile.position.Y = Projectile.position.Y - Projectile.height / 2;
            SoundEngine.PlaySound(SoundID.Grass, Projectile.position);

            int num4;
            for (int num413 = 0; num413 < 5; num413 = num4 + 1)
            {
                Dust.NewDust(new Vector2(Projectile.position.X, Projectile.position.Y), Projectile.width, Projectile.height, 40, 0f, 0f, 0, default, 1f);
                num4 = num413;
            }
            float numberProjectiles = Main.rand.Next(2, 4);
            for (int i = 0; i < numberProjectiles; i++)
            {
                Vector2 perturbedSpeed = new(Main.rand.Next(-4, 5), Main.rand.Next(-4, 5));
                if (Main.myPlayer == Projectile.owner)
                {
                    int leaf = Projectile.NewProjectile(Projectile.GetSource_Death(), Projectile.position.X + perturbedSpeed.X, Projectile.position.Y + perturbedSpeed.Y, perturbedSpeed.X, perturbedSpeed.Y, ProjectileID.Leaf, (int)(Projectile.damage * 0.66f), Projectile.knockBack, Projectile.owner, 0f, 0f);
                    Main.projectile[leaf].DamageType = DamageClass.Ranged;
                    Main.projectile[leaf].tileCollide = false;
                    Main.projectile[leaf].extraUpdates = 1;
                    Main.projectile[leaf].timeLeft = 30;
                }
            }
        }
    }
}